package com.zjhcsoft.util;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import oracle.sql.CLOB;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * 公共 有关的
 * 
 * @author dingwanli
 * @date 2012/5/3
 * @version 2.0
 */

public class CommonUtil {
	
	private static final Logger logger = Logger.getLogger(CommonUtil.class);
	
	public static List<Map<String, Object>> listMapToLowerCase(
			List<Map<String, Object>> list) {
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				list.set(i, mapToLowerCase(map));
			}
		}
		return list;
	}

	public static Map<String, Object> mapToLowerCase(Map<String, Object> inMap) {
		Map<String, Object> resultMap = null;
		if (inMap != null) {
			if (LinkedHashMap.class.isInstance(inMap)) {
				resultMap = new LinkedHashMap<String, Object>();
			} else if (HashMap.class.isInstance(inMap)) {
				resultMap = new HashMap<String, Object>();
			} else {
				return inMap;
			}
			for (Iterator<Entry<String, Object>> it = inMap.entrySet()
					.iterator(); it.hasNext();) {
				Entry<String, Object> e = it.next();
				String new_key = e.getKey().toLowerCase();
				resultMap.put(new_key, e.getValue());
			}
		}
		return resultMap;
	}
	/**
	 * 转换字符串，若为null返回为空串
	 * 
	 */
	public static String transform(String str){
		if(str==null){
			return "";
		}else{
			return str;
		}
	}
	
	/**
	 * 检验字符串是否为字符串
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		if (str == null || "".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}
	public static boolean isEmpty(Object str) {
		if (str == null) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isEmpty(Map<String, Object> map) {
		if (map == null || map.isEmpty()) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isEmpty(Long str) {
		if (str == null) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isEmpty(List<Object> list) {
		if (list == null || list.size() == 0) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isEmpty(String[] arr) {
		if (arr == null || arr.length == 0) {
			return true;
		} else {
			return false;
		}
	}

	public static String getNowTimeString() {
		return getNowTimeString("yyyyMMddHHmmss");
	}

	public static String getNowTimeString(String format) {
		return formatDate(new Date(), format);
	}
	
	public static String formatDate(Date date, String format) {
		SimpleDateFormat sf = new SimpleDateFormat(format);
		return sf.format(date);
	}
	
	public static String oracleClob2Str(CLOB clob) throws SQLException {
		return (clob != null ? clob.getSubString(1, (int) clob.length()) : null);
	}

	public static String changeEncode(String str, String fromEncode,
			String toEncode) throws UnsupportedEncodingException {
		String returnStr;
		returnStr = new String(str.getBytes(fromEncode), toEncode);
		return returnStr;
	}
	
	/**
	 * json数组字符串转List<String>
	 * @param jsonStr
	 * @return
	 */
	public static List<String> jsonStr2List(String jsonStr){
		List<String> list = new ArrayList<String>();
		try {
			JSONArray jsonList = new JSONArray(jsonStr);
			for(int i=0;i<jsonList.length();i++){
				list.add(jsonList.getString(i));
			}
		} catch (JSONException e) {
			logger.error(e.getMessage());
		}
		return list;
	}
	
	/**
	 * json数组字符串中第一个String
	 * @param jsonStr
	 * @return
	 */
	public static String jsonStr2First(String jsonStr){
		String str = "";
		try {
			JSONArray jsonList = new JSONArray(jsonStr);
			if(jsonList.length()>0){
				return jsonList.getString(0);
			}
		} catch (JSONException e) {
			logger.error(e.getMessage());
		}
		return str;
	}
}
